package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.BusinessProcess;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class BusProcessLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(BusProcessLayerReader.class);

	private final String BUS_PROC_FILE_NAME = "USCIS Process Decomposition - Level 4, 2.8.2010.xls";
	private String SHEET_NAME = "L2, L3, and L4";

	private String FUNC_COL_NUM = "B";
	private String SERIAL_COL_NUM = "C";
	private String NAME_COL_NUM = "D";
	
	private int MAX_BUS_PROCS = 500;
	
	public SOAData extractBusinessProcs(SOAData soaData) {
		loger.debug("Extracting Business Process layer Sheet...");
		HSSFWorkbook w = openFile(BUS_PROC_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);

		ArrayList<BusinessProcess> bProcess = new ArrayList<BusinessProcess>();
		soaData.bProcessList = bProcess;
		for (int n = 2; n < MAX_BUS_PROCS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String data = getCellStringValue(w, row, NAME_COL_NUM);
			if(Utils.isEmpty(data)) {
				loger.warn("Agent Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			BusinessProcess a = new BusinessProcess();
			a.id = Utils.getNextId();
			a.name = getCellStringValue(w, row, NAME_COL_NUM);
			a.function = getCellStringValue(w, row, FUNC_COL_NUM);
			a.serialNum = getCellStringValue(w, row, SERIAL_COL_NUM);
			bProcess.add(a);
		}
		closeAll(w);
		return soaData;
	}
}
